﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UcAsp.Models;
namespace UcAsp.Controllers
{
    public class SignController : Controller
    {
        //
        // GET: /Sign/
        private UcAspNetEntities entities = new UcAspNetEntities();
        public ActionResult Index(int? id)
        {
            Articles art = new Articles();
            int cate = 0;
            if (id != null)
            {
                art = entities.Articless.FirstOrDefault(o => o.ID == id);
                cate = art.CategorieID;
               
                if (art == null)
                    art = new Articles();
            }
            if (cate == 8 || cate == 27)
            {
                return View(art);
            }
            else {
                
                var query = from c in entities.Articless
                            join m in entities.Categoriess
                            on c.CategorieID equals m.ID
                            where m.ParentID == 11
                            orderby c.Rate descending
                            select c;
                IEnumerable<Articles> accessIDs = query.ToList();


                SelectList selectlist = new SelectList(accessIDs, "ID", "Title");
                ViewData["selectlist"] = selectlist;

                ViewData["Student"] = query.ToList();

                return View("SignStudent",art);

            }
        }
       
        public ActionResult SaveSign(Course model)
        {
            Message message = new Message();
            if (string.IsNullOrEmpty(model.TrueName))
            {
                message = new Message { Result = false,Messages="请输入真实姓名!" };
                return Json(message);
            }
            if(string.IsNullOrEmpty(model.Mobile))
            {
                message = new Message { Result = false, Messages = "请输入手机号!" };
                return Json(message);
            }               
            model.AddDate = DateTime.Now;
            entities.Courses.Add(model);
            entities.SaveChanges();
            message = new Message { Result = true };
            return Json(message);
        }

    }
}
